package com.zyd.shiro.business.controller.web;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.User;
import com.zyd.shiro.business.entity.WxUser;
import com.zyd.shiro.business.service.PakWxUserService;
import com.zyd.shiro.business.service.SysResourcesService;
import com.zyd.shiro.business.service.SysUserService;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.framework.object.PageResult;
import com.zyd.shiro.framework.object.ResponseVO;
import com.zyd.shiro.persistence.beans.PakWxUser;
import com.zyd.shiro.persistence.beans.SysUser;
import com.zyd.shiro.util.ResultUtil;
import com.zyd.shiro.utils.ExcelUtil;
import com.zyd.shiro.utils.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.Serializable;
import java.util.Date;
import java.util.List;


@RestController
@Slf4j
@RequestMapping("/web/wxUser")
public class WebWxUserController {
    @Autowired
    private PakWxUserService wxUserService;

    @Autowired
    private SysUserService userService;



    @GetMapping(value = "/login")
    public ResponseVO login(String openid) {
        PakWxUser wxUser = wxUserService.findByOpenid(openid);
        if(wxUser==null){
            return ResultUtil.error("该用户未绑定！");
        }

        UsernamePasswordToken token = new UsernamePasswordToken(wxUser.getUsername(), wxUser.getPassword());
        //获取当前的Subject
        Subject currentUser = SecurityUtils.getSubject();
        try {
            // 在调用了login方法后,SecurityManager会收到AuthenticationToken,并将其发送给已配置的Realm执行必须的认证检查
            // 每个Realm都能在必要时对提交的AuthenticationTokens作出反应
            // 所以这一步在调用login(token)方法时,它会走到xxRealm.doGetAuthenticationInfo()方法中,具体验证方式详见此方法
            User user = userService.getByUserName(wxUser.getUsername());
            currentUser.login(token);
            //token信息
            Subject subject = SecurityUtils.getSubject();
            Serializable tokenId = subject.getSession().getId();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("user",user);
            jsonObject.put("token",tokenId);
            return ResultUtil.success("登录成功！",jsonObject);
        } catch (Exception e) {
            log.error("登录失败，用户名[{}]", wxUser.getUsername(), e);
            token.clear();
            return ResultUtil.error(e.getMessage());
        }
    }


    @PostMapping(value = "/bind")
    public ResponseVO bind(String username,String password,String openid) {
        PakWxUser wxUser = wxUserService.findByOpenid(openid);
        if(wxUser!=null){
            return ResultUtil.error("该用户已绑定！");
        }
        wxUser = wxUserService.findByUsername(username);
        if(wxUser!=null){
            return ResultUtil.error("该账号用户已绑定！");
        }
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        //获取当前的Subject
        Subject currentUser = SecurityUtils.getSubject();
        try {
            // 在调用了login方法后,SecurityManager会收到AuthenticationToken,并将其发送给已配置的Realm执行必须的认证检查
            // 每个Realm都能在必要时对提交的AuthenticationTokens作出反应
            // 所以这一步在调用login(token)方法时,它会走到xxRealm.doGetAuthenticationInfo()方法中,具体验证方式详见此方法
            User user = userService.getByUserName(username);
            currentUser.login(token);
            //openid绑定user
            wxUser = new PakWxUser();
            wxUser.setUsername(username);
            wxUser.setPassword(password);
            wxUser.setUser(user.getId());
            wxUser.setOpenid(openid);
            wxUserService.increase(wxUser);

            //token信息
            Subject subject = SecurityUtils.getSubject();
            Serializable tokenId = subject.getSession().getId();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("user",user);
            jsonObject.put("token",tokenId);
            return ResultUtil.success("登录成功！",jsonObject);
        } catch (Exception e) {
            log.error("登录失败，用户名[{}]", username, e);
            token.clear();
            return ResultUtil.error(e.getMessage());
        }
    }


}